//
//  EKStepsFlowView.m
//  EKViews_Example
//
//  Created by zach on 2018/11/10.
//  Copyright © 2018年 zwc. All rights reserved.
//

#import "EKStepsFlowView.h"
#import "Masonry.h"

#define kNumSize 20
#define kLineWidth 30
#define kNumTitleSpace 12
#define kFocusViewWidth 30
#define kNumLineSpace 8
#define kOFFSET  (kNumSize + kNumLineSpace*2 + kLineWidth)

#define kTitleFont [UIFont systemFontOfSize:14]

#define kFocusViewTag   1
#define kNumTag0        100
#define kTitleTag0      200
#define kLineTag0       300
#define kImageTag0      1000

#define kSelectedColor   [UIColor colorWithRed:0.00 green:0.69 blue:0.94 alpha:1.00]
#define kDeselectedColor [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1.00]
#define kFailedColor     [UIColor colorWithRed:1.00 green:0.54 blue:0.30 alpha:1.00]

@interface EKStepsFlowView () {
}
@property (nonatomic, strong) UIScrollView *contentView;
@end

@implementation EKStepsFlowView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self commonInit];
}

- (instancetype)initWithSteps:(NSArray<NSString *>*)steps {
    if (self= [super init]) {
        _steps = steps;
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    _contentView = [[UIScrollView alloc] init];
    _contentView.showsVerticalScrollIndicator = NO;
    _contentView.showsHorizontalScrollIndicator = NO;
    _contentView.alwaysBounceVertical = NO;
    self.clipsToBounds = YES;
    
    [self addSubview:_contentView];
    
    if (self.steps)
        [self buildUI];
}

- (void)buildUI {
    
    __weak typeof(self) weakSelf = self;
    
    UIView *focusBgV = [[UIView alloc] init];
    focusBgV.backgroundColor = [UIColor colorWithRed:0.75 green:0.93 blue:0.99 alpha:1.00];
    focusBgV.layer.cornerRadius = kFocusViewWidth/2;
    focusBgV.layer.masksToBounds = YES;
    focusBgV.tag = kFocusViewTag;
    [weakSelf.contentView addSubview:focusBgV];

    //生成步骤视图
    [_steps enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel *lblTitle = [[UILabel alloc] init];
        lblTitle.text = title;
        lblTitle.textColor = kDeselectedColor;
        lblTitle.textAlignment = NSTextAlignmentCenter;
        lblTitle.font = kTitleFont;
        lblTitle.tag = kTitleTag0 + idx;
        [weakSelf.contentView addSubview:lblTitle];
        
        UILabel *lblNum = [[UILabel alloc] init];
        lblNum.text = [NSString stringWithFormat:@"%lu", idx+1];
        lblNum.textColor = [UIColor whiteColor];
        lblNum.backgroundColor = kDeselectedColor;
        lblNum.font = [UIFont systemFontOfSize:15];
        lblNum.textAlignment = NSTextAlignmentCenter;
        lblNum.layer.cornerRadius = kNumSize/2;
        lblNum.layer.masksToBounds = YES;
        lblNum.tag = kNumTag0 + idx;
        [weakSelf.contentView addSubview:lblNum];
        
        if (idx < weakSelf.steps.count-1) {
            UIView *line = [[UIView alloc] init];
            line.tag = kLineTag0 + idx;
            line.backgroundColor = kDeselectedColor;
            [weakSelf.contentView addSubview:line];
        }
    }];
}
    
- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutUI];
}

- (void)layoutUI {
    __weak typeof(self) weakSelf = self;
    [_steps enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel *lblNum = [weakSelf.contentView viewWithTag:kNumTag0+idx];
        UIView *line = [weakSelf.contentView viewWithTag:kLineTag0+idx];
        UILabel *lblTitle = [weakSelf.contentView viewWithTag:kTitleTag0+idx];
        
        [lblNum mas_remakeConstraints:^(MASConstraintMaker *make) {
            CGFloat x = [self getNumXWithIndex:idx];
            make.left.mas_equalTo(x);
            make.top.mas_equalTo((kFocusViewWidth-kNumSize)/2);
            make.size.mas_equalTo(CGSizeMake(kNumSize, kNumSize));
        }];
        
        [lblTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(lblNum);
            make.top.equalTo(lblNum.mas_bottom).offset(kNumTitleSpace);
            make.size.mas_greaterThanOrEqualTo(CGSizeZero);
        }];

        [line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(lblNum);
            make.left.equalTo(lblNum.mas_right).offset(kNumLineSpace);
            make.size.mas_equalTo(CGSizeMake(kLineWidth, 1));
        }];
        
    }];

    UIView *curNumLbl = [self.contentView viewWithTag:kNumTag0+self.currentStep];
    UIView *focusView = [self.contentView viewWithTag:kFocusViewTag];
    [focusView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(curNumLbl);
        make.size.mas_equalTo(CGSizeMake(kFocusViewWidth, kFocusViewWidth));
    }];
    
    self.contentView.frame = CGRectMake(0, 10, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)-10);
    self.contentView.contentSize = [self getContentSize];
}

- (void)setCurrentStep:(NSUInteger)currentStep {
    
    if (currentStep > _steps.count-1) {
        return;
    }
    _currentStep = currentStep;
    
    __weak typeof(self) weakSelf = self;
    [_steps enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *lblNum = [weakSelf.contentView viewWithTag:kNumTag0+idx];
        UIView *line = [weakSelf.contentView viewWithTag:kLineTag0+idx-1];
        UILabel *lblTitle = [weakSelf.contentView viewWithTag:kTitleTag0+idx];

        if (idx <= currentStep) {
            // 全部高亮
            lblNum.backgroundColor = lblTitle.textColor = kSelectedColor;
            line.backgroundColor = kSelectedColor;
        }
        else {
            // 置灰
//            lblNum.backgroundColor = lblTitle.textColor = kDeselectedColor;
//            line.backgroundColor = kDeselectedColor;
        }
    }];
    
    // 判断当前步骤内容是否超出显示
    CGFloat titleW = [self getSizeWithString:_steps[_currentStep] andSize:CGSizeMake(200, 200) andFont:kTitleFont].width;
    CGFloat maxX = [self getNumXWithIndex:_currentStep] + (kNumSize+ titleW)/2 + 10;
    if (maxX > (self.bounds.size.width + _contentView.contentOffset.x)) {
        [_contentView setContentOffset:CGPointMake(kOFFSET+_contentView.contentOffset.x, 0) animated:YES];
    }
}

- (void)setSteps:(NSArray<NSString *> *)steps {
    _steps = [steps copy];
    [self buildUI];
    [self layoutIfNeeded];
}
    
- (CGFloat)defaultViewHeight {
    return (kFocusViewWidth-kNumSize)+kNumSize+kNumTitleSpace+18;
}

- (void)setSuccessForStep:(NSInteger)step {
    [self setStatusForStep:step withImage:@"icon_returncar_success"];
    UILabel *lblTitle = [self.contentView viewWithTag:kTitleTag0+step];
    lblTitle.textColor = kSelectedColor;
}
    
- (void)setFailedForStep:(NSInteger)step; {
    [self setStatusForStep:step withImage:@"icon_returncar_fail"];
    UILabel *lblTitle = [self.contentView viewWithTag:kTitleTag0+step];
    lblTitle.textColor = kFailedColor;
}

- (void)setResetForStep:(NSInteger)step {
    UIImageView *imgv = [self.contentView viewWithTag:kImageTag0+step];
    [imgv removeFromSuperview];
    
    UIView *lblNum = [self.contentView viewWithTag:kNumTag0+step];
    lblNum.backgroundColor = kSelectedColor;
    
    UILabel *lblTitle = [self.contentView viewWithTag:kTitleTag0+step];
    lblTitle.textColor = kSelectedColor;
}
    
-(void)setStatusForStep:(NSInteger)step withImage:(NSString *)img {
    
    UIImageView *imgv = [self.contentView viewWithTag:kImageTag0+step];
    if (!imgv) {
        UIView *lblNum = [self.contentView viewWithTag:kNumTag0+step];
        lblNum.backgroundColor = [UIColor whiteColor];
        
        UIView *focusV = [self.contentView viewWithTag:kFocusViewTag];
        focusV.backgroundColor = [UIColor whiteColor];
        
        imgv = [[UIImageView alloc] initWithFrame:lblNum.frame];
        imgv.frame = lblNum.frame;
        [_contentView addSubview:imgv];
        imgv.tag =kImageTag0+step;
    }
    imgv.image = [UIImage imageNamed:img];
}

#pragma mark - Helper

- (CGFloat)getNumXWithIndex:(NSInteger)idx {
    CGFloat wHead = [self getSizeWithString:_steps[0] andSize:CGSizeMake(200, 200) andFont:kTitleFont].width;
    return 10+(wHead-kNumSize)/2+idx*kOFFSET;
}
    
- (CGSize)getContentSize {
    //受首尾两个标题影响
    CGFloat wHead = [self getSizeWithString:_steps[0] andSize:CGSizeMake(500, 500) andFont:kTitleFont].width;
    CGFloat wTail = [self getSizeWithString:_steps[_steps.count-1] andSize:CGSizeMake(500, 500) andFont:kTitleFont].width;
    
    CGFloat contentWidth = 20 + kOFFSET*(_steps.count-1) + (wHead+wTail)/2;
    return CGSizeMake(contentWidth, CGRectGetHeight(self.contentView.bounds));
}
    
- (CGSize)getSizeWithString:(NSString *)title andSize:(CGSize)size andFont:(UIFont *)font {
    CGSize titleS = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return titleS;
}
    
@end
